//
//  BaseViewController.m
//  BCBDemo
//
//  Created by wukong on 2018/9/18.
//  Copyright © 2018年 BCB. All rights reserved.
//

#import "BaseViewController.h"


static MBProgressHUD *hud;

@interface BaseViewController ()

//@property (strong, nonatomic) UIView  *backGroundView; //导航栏下面的占位view

@end

@implementation BaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = RGBColor(21, 16, 44);
    self.fd_prefersNavigationBarHidden = YES;
    [self private_initUI];
    self.NavStyleType = RGCustomNavStyleType_black_noLine;
    
}


#pragma mark - UI

-(void) private_initUI{
    
//    [self.view addSubview:self.backGroundView];
    [self.view addSubview:self.customContentView];
    [self.view addSubview:self.customNavBar];
    
//    [_backGroundView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.right.mas_equalTo(0);
//        make.top.mas_equalTo(0);
//
//        if ([self iPhoneNotchScreen]) {
//
//            make.height.mas_equalTo(88);
//        }else{
//            make.height.mas_equalTo(64);
//        }
//    }];
    CGFloat height = [self iPhoneNotchScreen] ? 88 : 64;
    [_customNavBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(0);
        make.height.mas_equalTo(height);
    }];
    
    [_customContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view).mas_offset(height);
        make.left.right.mas_equalTo(0);
        if (@available(iOS 11.0, *)) {
            make.bottom.mas_equalTo(self.view.mas_safeAreaLayoutGuideBottom);
        }else{
            make.bottom.mas_equalTo(0);
        }
    }];
    
   
}

- (CGFloat) navagationBarHeight {
    return self.navigationController.navigationBar.frame.size.height;
}

- (void) setNavigationBarTitleColor:(UIColor *)color {
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:color}];
}

#pragma mark - public
-(void) setNavStyleType:(RGCustomNavStyleType)NavStyleType{
    _NavStyleType = NavStyleType;
    switch (NavStyleType) {
        case RGCustomNavStyleType_default:
        {
            _customNavBar.backgroundColor = [UIColor clearColor];
            _customNavBar.isShowBottomLine = NO;
            [_customContentView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(0);
            }];
        }
            break;
        case RGCustomNavStyleType_black:
        {
            _customNavBar.backgroundColor = [UIColor blackColor];
            _customNavBar.isShowBottomLine = YES;
            [_customContentView mas_updateConstraints:^(MASConstraintMaker *make) {
                if ([self iPhoneNotchScreen]) {
                    make.top.mas_equalTo(88);
                }else{
                    make.top.mas_equalTo(64);
                }
            }];
        }
            break;
        case RGCustomNavStyleType_black_noLine:
        {
            _customNavBar.backgroundColor = [UIColor blackColor];
            _customNavBar.isShowBottomLine = NO;
            [_customContentView mas_updateConstraints:^(MASConstraintMaker *make) {
                if ([self iPhoneNotchScreen]) {
                    make.top.mas_equalTo(88);
                }else{
                    make.top.mas_equalTo(64);
                }
            }];
        }
            break;
    }
}

-(void) setIsHiddenNavBar:(BOOL)isHiddenNavBar{
    _isHiddenNavBar = isHiddenNavBar;
    if(isHiddenNavBar){
        self.customNavBar.hidden = YES;
        [_customContentView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(0);
        }];
    }else{
        self.customNavBar.hidden = NO;
        [_customContentView mas_updateConstraints:^(MASConstraintMaker *make) {
            if ([self iPhoneNotchScreen]) {
                make.top.mas_equalTo(88);
            }else{
                make.top.mas_equalTo(64);
            }
        }];
    }
}

#pragma mark -protocol

-(void)showLoading:(NSString *)msg {
    dispatch_async(dispatch_get_main_queue(), ^{
        if (hud && !hud.removeFromSuperViewOnHide) {
            [hud hideAnimated:NO];
            hud = nil;
        }
        hud = [MBProgressHUD showActivityMessageInView:msg];
    });
    
}

-(void)dismissLoading {
    dispatch_async(dispatch_get_main_queue(), ^{
        if (hud) {
            [hud hideAnimated:YES];
            hud = nil;
        }
    });
    
}

-(void) showTextMessageWithMessage:(NSString *)msg{
    if(msg.length == 0){
        dispatch_async(dispatch_get_main_queue(), ^{
            if (hud) {
                [hud hideAnimated:YES];
                hud = nil;
            }
        });
        
    }else{
        dispatch_async(dispatch_get_main_queue(), ^{
            //        [self dismissLoading];
            [MBProgressHUD showTipMessageInWindow:msg];
        });
    }
}



- (UIStatusBarStyle)preferredStatusBarStyle{
    return _StatusBarStyle;
}
//动态更新状态栏颜色
-(void)setStatusBarStyle:(UIStatusBarStyle)StatusBarStyle{
    _StatusBarStyle = StatusBarStyle;
    [self setNeedsStatusBarAppearanceUpdate];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
}


#pragma mark -  屏幕旋转
-(UIInterfaceOrientationMask)supportedInterfaceOrientations{
    //当前支持的旋转类型
    return UIInterfaceOrientationMaskPortrait;
}

- (BOOL)shouldAutorotate{
    // 是否支持旋转
    return NO;
}

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    // 默认进去类型
    return   UIInterfaceOrientationPortrait;
}

#pragma mark - Common Method
///刘海屏判断
-(BOOL)iPhoneNotchScreen {
    CGFloat iPhoneNotchDirectionSafeAreaInsets = 0;
    if (@available(iOS 11.0, *)) {
        UIEdgeInsets safeAreaInsets = [UIApplication sharedApplication].windows[0].safeAreaInsets;
        switch ([UIApplication sharedApplication].statusBarOrientation) {
            case UIInterfaceOrientationPortrait:{
                iPhoneNotchDirectionSafeAreaInsets = safeAreaInsets.top;
            }
                break;
            case UIInterfaceOrientationLandscapeLeft:{
                iPhoneNotchDirectionSafeAreaInsets = safeAreaInsets.left;
            }
                break;
            case UIInterfaceOrientationLandscapeRight:{
                iPhoneNotchDirectionSafeAreaInsets = safeAreaInsets.right;
            }
                break;
            case UIInterfaceOrientationPortraitUpsideDown:{
                iPhoneNotchDirectionSafeAreaInsets = safeAreaInsets.bottom;
            }
                break;
            default:
                iPhoneNotchDirectionSafeAreaInsets = safeAreaInsets.top;
                break;
        }
    } else {
        return NO;
    }
    return iPhoneNotchDirectionSafeAreaInsets > 20;
}

#pragma mark - getter

-(RGCustomNavBarView *) customNavBar{
    if(!_customNavBar){
        _customNavBar = [[RGCustomNavBarView alloc] init];
    }
    return _customNavBar;
}

//-(UIView *)backGroundView{
//    if(!_backGroundView){
//        _backGroundView = [[UIView alloc]init];
//        _backGroundView.backgroundColor = [UIColor clearColor];
//    }
//    return _backGroundView;
//}

-(UIView *)customContentView{
    if(!_customContentView){
        _customContentView = [[UIView alloc]init];
    }
    return _customContentView;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any - (void)dismissLoading {
    
}




@end
